<%--
  Created by IntelliJ IDEA.
  User: 52203
  Date: 2018/12/27
  Time: 11:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <base href="<%=request.getContextPath()%>/"/>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@include file="../layUI.jsp"%>
</head>
<script type="text/javascript" src="static/js/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
    var id ;
    /*显示数据*/
    $(function () {
        id='${param.id}';
        $.getJSON("/doCus",{"action":"selByID","id":id},callback);
        function callback(data) {
            $("[name=id]").val(data[0].id);
            $("[name=cusName]").val(data[0].cusName);
            $("[name=cusLoginName]").val(data[0].cusLoginName);
            $("[name=cusEmail]").val(data[0].cusEmail);
            if(data[0].cusSex=="男"){
                $("[name=cusSex]").eq(0).prop("checked",true);
            }else if(data[0].cusSex=="女"){
                $("[name=cusSex]").eq(1).prop("checked",true);
            }
            $("[name=cusHobby]").val(data[0].cusHobby);
            $("[name=cusCode]").val(data[0].cusCode);
            $("[name=cusBirthday]").val(data[0].cusBirthday);

        }
    })
    /*保存信息*/
    function saves() {
        //非空验证
        var flag = true;
        $(".notNull").each(function () {
            var name = $(this).attr("name");
            if($(this).val()==""){
                alert($(this).attr('notNull')+"不能为空");
                flag =false;
                return;
            }
        })
       var cusEmail = $("[name=cusEmail]").val();
       var re = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
        if (!re.test(cusEmail)) {
            alert("邮箱不正确")
            flag = false;
            return;;
        }
        var cusBirthday = $("[name=cusBirthday]").val();
        var myDate = new Date();
        var now = myDate.valueOf();
        var time = new Date(cusBirthday).valueOf();
        if(time>now){
            alert("日期不正确")
            flag = false;
            return;;
        }
        if(flag){
            //得到所有值，并转为字符串
            var queryString = $("form").serialize();
            $.getJSON("/doCus?action=cusUpdate&id=" + id, queryString, callback);
            function callback(data) {
                if (data.flag == "true") {
                    alert("修改成功!");
                    location.href="/page/customer/cusQuery.jsp";
                } else {
                    alert("修改失败!");
                    location.href="/page/customer/cusUpdate.jsp";
                }
            }
        }
    }

</script>
<body>
    <form action="doCus?action=cusUpdate" method="post">
        ID<input type="text" value="" name="id" readonly>
        姓名<input type="text" value="" name="cusName" readonly>
        登录账号<input type="text" value="" name="cusLoginName" class="notNull" notNull="登录账号" readonly>
        Email<input type="text" value="" name="cusEmail"  class="notNull" notNull="邮箱"><br>
        性别<input type="radio" value="男" name="cusSex"   class="notNull" notNull="性别">男
        <input type="radio" value="女" name="cusSex" >女 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        爱好<input type="text" value="" name="cusHobby"  class="notNull" notNull="爱好">
        证件号<input type="text" value="<" name="cusCode"  class="notNull" notNull="证件号">
        生日<input type="date" value="" name="cusBirthday"  class="notNull" notNull="生日"><br>
        <input type="button" value="修改完成" class="layui-btn" onclick=" saves()">
    </form>
</body>
</html>
